package com.shop.controller;

import com.github.pagehelper.PageInfo;
import com.shop.base.BaseController;
import com.shop.entity.Product;
import com.shop.result.ResultVO;
import com.shop.service.ProductService;
import com.shop.vo.ProductAggregationVO;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController extends BaseController {
    private final ProductService productService;

    /**
     * 查询所有商品
     * @param page 当前页码
     * @param pageSize 每页记录数
     * @return
     */
    @GetMapping("/listProduct")
    public ResultVO<Object> listProduct(@RequestParam("pageNo") Integer page, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<Product> pageInfo = productService.listProduct(page, pageSize);
        return success(pageInfo);
    }
    /**
     * 查询所有商品分类映射
     * @param pageNum 当前页码
     * @param pageSize 每页记录数
     * @return
     */
    @PostMapping("/listProductCategoryMappings")
    public ResultVO<Object> listProductCategoryMappings(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize) {
        return success(productService.listProductCategoryMappings(pageNum, pageSize));
    }
    /**
     * 聚合查询商品
     * @return
     */
    @GetMapping("/listProductAggregation")
    public ResultVO<Object> listProductAggregation(@RequestParam("pageNo") Integer pageNo, @RequestParam("pageSize") Integer pageSize) {
        PageInfo<ProductAggregationVO> pageInfo = productService.listProductAggregation(pageNo, pageSize);
        return success(pageInfo);
    }

    @GetMapping("/listAllProduct")
    public ResultVO<Object> listAllProduct() {
        PageInfo<ProductAggregationVO> pageInfo = productService.listAllProduct();
        return success(pageInfo);
    }
    /**
     * 查找商品
     * @param id 商品id
     * @return 商品
     */
    @GetMapping("/getProductById")
    public ResultVO<Object> getProductById(@RequestParam("id") Integer id) {
        return success(productService.getProductById(id));
    }

    /**
     * 添加商品
     * @param product 商品
     * @return 添加结果
     */
    @PostMapping("/addProduct")
    public Integer addProduct(@RequestBody Product product) {
        return productService.addProduct(product);
    }

    /**
     * 删除商品
     * @param id 商品id
     * @return 删除结果
     */
    @PostMapping("/deleteProduct")
    public ResultVO<Object> deleteProduct(@RequestParam("productId") Integer id) {
        productService.deleteProduct(id);
        return success();
    }
    /**
     * 更新商品
     * @param product 商品
     * @return 更新结果
     */
    @PostMapping("/updateProduct")
    public ResultVO<Object> updateProduct(@RequestBody Product product) {
        productService.updateProduct(product);
        return success();
    }
    /**
     * 查询商品总数
     * @return 商品总数
     */
    @GetMapping("/countProduct")
    public ResultVO<Object> getTotal() {
        return success(productService.getTotal());
    }
}
